﻿#include <Configuration.h>
#include <Resources.h>
#include <Constants.h>
#include "SettingsDialog.h"
#include "EditorSettingsWidget.h"
#include "GeneralSettingsWidget.h"
#include "ui_SettingsDialog.h"

namespace Domy {

/**
 * @brief The SettingsPageIndex enum
 * @note  设置页面索引
 */
enum SettingsPageIndex {
    SettingsPage_Editor = 0,
    SettingsPage_General,
};

SettingsDialog::SettingsDialog(QWidget *parent)
    : QDialog(parent),
    m_ui(new Ui::SettingsDialog),
    m_editor_settings(new EditorSettingsWidget(this)),
    m_general_settings(new GeneralSettingsWidget(this))
{
    m_ui->setupUi(this);

    this->setWindowTitle(tr("Settings"));

    this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);

    m_ui->pushButton_apply->setEnabled(false);

    m_ui->tabWidget_settings->addTab(m_editor_settings, GET_ICON("editor"), tr("Editor"));

    m_ui->tabWidget_settings->addTab(m_general_settings, GET_ICON("general"), tr("General"));

    connect(m_ui->pushButton_ok, SIGNAL(clicked()), this, SLOT(slotOnOKBtnClicked()));
    connect(m_ui->pushButton_cancel, SIGNAL(clicked()), this, SLOT(close()));
    connect(m_ui->pushButton_apply, SIGNAL(clicked()), this, SLOT(slotOnApplyBtnClicked()));
    connect(m_ui->pushButton_default, SIGNAL(clicked()), this, SLOT(slotOnRestoreDefault()));

    connect(m_editor_settings, SIGNAL(signalSettingsChanged()), this, SLOT(slotOnSettingsChanged()));
    connect(m_general_settings, SIGNAL(signalSettingsChanged()), this, SLOT(slotOnSettingsChanged()));
}

SettingsDialog::~SettingsDialog()
{
    delete m_ui;
}

void SettingsDialog::slotOnSettingsChanged()
{
    m_ui->pushButton_apply->setEnabled(true);
}

void SettingsDialog::slotOnOKBtnClicked()
{
    this->applySettings();

    this->close();
}

void SettingsDialog::slotOnApplyBtnClicked()
{
    this->applySettings();

    m_ui->pushButton_apply->setEnabled(false);
}

void SettingsDialog::slotOnRestoreDefault()
{
    this->restoreSettings();

    m_ui->pushButton_apply->setEnabled(true);
}

void SettingsDialog::applySettings()
{
    m_editor_settings->apply();

    m_general_settings->apply();

    emit signalUpdateSettings();
}

void SettingsDialog::restoreSettings()
{
    m_editor_settings->restoreDefault();

    m_general_settings->restoreDefault();
}

} // namespace Domy
